package com.yuanyefu.MQlearn;

import com.aliyun.openservices.ons.api.*;
import com.yuanyefu.Util.ByteObjectUtil;

import java.util.Date;
import java.util.Properties;

/**
 * Created by 339814 on 2017/9/18.
 */
public class ConsumerTest {
    private static final String TOPIC = "m5173test";
    private static final String TAG = "TagA";
    public static void main(String[] args){
        Properties properties = new Properties();
        properties.put(PropertyKeyConst.ConsumerId,"CID-m5173test");
        properties.put(PropertyKeyConst.AccessKey,"GRWdJPdv2nQMCybv");
        properties.put(PropertyKeyConst.SecretKey,"WqMdhwY7GFaj4Kf4G9wRdkKVeRZQr3");
        properties.put(PropertyKeyConst.ONSAddr,
                "http://onsaddr-internet.aliyun.com/rocketmq/nsaddr4client-internet");
        Consumer consumer = ONSFactory.createConsumer(properties);
        consumer.subscribe(TOPIC, "order", new MessageListener() {
            @Override
            public Action consume(Message message, ConsumeContext consumeContext) {

                System.out.println(new Date() + "Receive message, Topic is:" + TOPIC + ", MsgId is:" + message.getMsgID());
                System.out.println("message: "+ ByteObjectUtil.byteArrayToObject(message.getBody()));
                //如果想测试消息重投的功能,可以将Action.CommitMessage 替换成Action.ReconsumeLater
                return Action.CommitMessage;
            }
        });
        consumer.start();
        System.out.println("Consumer Started");
    }
}
